﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Threading;
using System.Security.Principal;
using Locator.Products;
using DataAccess.UnitTests;

namespace Products.Tests
{
    [TestFixture]
    public class ProductsControllerTests : DataTestBase
    {
        [Test]
        public void GetProductsForUser()
        {
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
            using (ProductsController controller = new ProductsController(this.UnitOfWork))
            {
                var products = controller.GetProductsForCurrentUser();
                Assert.AreEqual(3, products.Count);
                Assert.IsTrue(products[0].UsedByUser);
                Assert.AreEqual("Heineken 6x330.00 ml", products[0].Name);
                Assert.IsTrue(products[1].UsedByUser);
                Assert.AreEqual("Mike Weir Wine Chardonnay (V) 750 ml", products[1].Name);
            }
        }

        [Test]
        public void GetProductsByName()
        {
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
            using (ProductsController controller = new ProductsController(this.UnitOfWork))
            {
                var products = controller.FindProductsContainingName("Heine");
                Assert.AreEqual(1, products.Count);
                Assert.IsTrue(products[0].UsedByUser);
                products = controller.FindProductsContainingName("olde");
                Assert.AreEqual(1, products.Count);
                Assert.IsFalse(products[0].UsedByUser);
            }
        }

        [Test]
        public void GetProductsBySubstanceId()
        {
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
            using (ProductsController controller = new ProductsController(this.UnitOfWork))
            {
                var products = controller.FindProductsContainingSubstance(80024);
                Assert.AreEqual(1, products.Count);
                Assert.IsTrue(products[0].UsedByUser);
                products = controller.FindProductsContainingSubstance(81491);
                Assert.AreEqual(1, products.Count);
                Assert.IsFalse(products[0].UsedByUser);
            }
        }

        [Test]
        public void GetProductsBySubstanceName()
        {
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
            using (ProductsController controller = new ProductsController(this.UnitOfWork))
            {
                var products = controller.FindProductsContainingName("heineken");
                Assert.AreEqual(1, products.Count);
                Assert.IsTrue(products[0].UsedByUser);
                Assert.AreEqual("Heineken 6x330.00 ml", products[0].Name);
                products = controller.FindProductsContainingName("Olde English");
                Assert.AreEqual(1, products.Count);
                Assert.IsFalse(products[0].UsedByUser);
                Assert.AreEqual("Olde English 6x330.00 ml", products[0].Name);
            }
        }

        [Test]
        public void AllSubstances()
        {
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
            using (ProductsController controller = new ProductsController(this.UnitOfWork))
            {
                var substances = controller.GetAllSubstances();
                Assert.AreEqual(4, substances.Count);
            }
        }

        [Test]
        public void AddAndDeleteProductForUser()
        {
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
            using (ProductsController controller = new ProductsController(this.UnitOfWork))
            {
                var usersProducts = controller.GetProductsForCurrentUser();
                Assert.AreEqual(3, usersProducts.Count);
                controller.AddProductForCurrentUser(100);
                usersProducts = controller.GetProductsForCurrentUser();
                Assert.AreEqual(4, usersProducts.Count);
                controller.DeleteProductForCurrentUser(100);
                usersProducts = controller.GetProductsForCurrentUser();
                Assert.AreEqual(3, usersProducts.Count);
            }
        }

        [Test]
        public void AllProducts()
        {
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
            using (ProductsController controller = new ProductsController(this.UnitOfWork))
            {
                var products = controller.GetAllProducts();
                Assert.AreEqual(4, products.Count);
                foreach (var product in products)
                {
                    if (product.Id == 18 || product.Id == 26 || product.Id == 99)
                    {
                        Assert.IsTrue(product.UsedByUser);
                    }
                    else
                    {
                        Assert.IsFalse(product.UsedByUser);
                    }
                }
            }
        }
    }
}
